package com.ly.hotel.view.fragments.intro;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.model.Serve;
import com.ly.hotel.util.ImageUtil;

/**
 * Created by SHUBEN on 2018/6/6.
 */

public class IntroDetailsDialog extends Dialog {

    private Context mContext;
    private ImageView detailImg;
    private WebView detailWeb;

    public IntroDetailsDialog(Context context, int theme) {
        super(context, theme);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.intro_details_dialog);
        detailImg = findViewById(R.id.detail_img);
        detailWeb = findViewById(R.id.detail_content);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // chromium, enable hardware acceleration
            detailWeb.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        } else {
            // older android version, disable hardware acceleration
            detailWeb.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
        detailWeb.getSettings().setDisplayZoomControls(false);
        detailWeb.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });
        detailWeb.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return true;
            }
        });
    }

    //设置数据
    public void setServe(Serve serve){
        String imgPath = MyApplication.getHostResUrl() + "upload/"
                + serve.getAd();
        ImageUtil.displayImage(mContext, imgPath,detailImg);
        detailWeb.loadDataWithBaseURL(null, getUrl(serve.getContent()), "text/html", "utf-8", null);
    }

    private String getUrl(String url){
        return "<html><head><meta http-equiv=\"content-type\"content=\"text/html;charset=utf-8\"></head><body style=\"background-color:#EEE8D5;\">"
                + url + "</body></html>";
    }
}
